{% extends 'layout.html' %}
{% load static %}
{% block content %}
    <div class="panel panel-default">
      <div class="panel-heading">折线图</div>
      <div class="panel-body">
        <div id="m1" style="width: 600px;height:300px;"></div>
      </div>
   </div>
    <div class="row">
        <div class="col-sm-8">
            <div class="panel panel-default">
      <div class="panel-heading">柱状图</div>
      <div class="panel-body">
        <div id="m2" style="width: 600px;height:600px;"></div>
      </div>
   </div>
        </div>
        <div class="col-sm-4">
            <div class="panel panel-default">
                <div class="panel-heading">饼图</div>
                <div class="panel-body">
                <div id="m3" style="width: 100%;height: 600px;"></div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'js/echarts.js' %}"></script>
    <script type="text/javascript">
        $(function (){
            initLine();
            initBar();
            intitPie();
        })

        /**
         * 折线图
        */
        function initLine(){
        var chartDom = document.getElementById('m1');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
              xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  data: [150, 230, 224, 218, 135, 147, 260],
                  type: 'line'
                }
              ]
            };
            $.ajax({
                'url':"/chart/line/",
                'type':'get',
                'dataType':'json',
                success:function (res){
                    if(res.status){
                        option.xAxis.data = res.xAxis
                        option.series[0].data = res.series
                        option && myChart.setOption(option)
                    }
                }
            });

        }

        /**
         * 柱状图
         */
        function initBar(){


      // 指定图表的配置项和数据

      //发送请求给后台

            $.ajax({
                url:"/chart/bar/",
                type:"get",
                dataType:"json",
                success:function (res){
                    var myChart = echarts.init(document.getElementById('m2'));
                      var option = {
                          title: {
                                text: '电脑配置',
                                bottom:0,
                                },
                          tooltip: {},
                          legend: {
                                    data: ['价格'],
                                    bottom:0,
                                    },
                              xAxis: {
                                type: 'category',
                                data: []
                              },
                              yAxis: {
                                type: 'value'
                              },
                              series: [
                                {
                                  name:"价格",
                                  data: [],
                                  type: 'bar'
                                }
                              ]
                            };
                    //将后台的数据返给option
                    //option.xAxis.data= res.data.tietle_list;
                    //option.series.data= res.data.price_list;
                    option.xAxis.data= res.data.tietle_list
                    option.series[0].data= res.data.price_list;
                    if(res.status){
                        // 使用刚指定的配置项和数据显示图表。
                         myChart.setOption(option);
                    }
                }
            })

        }

        /**
         * 饼状图
         */
        function intitPie(){
            $.ajax({
                'url':'/chart/pie/',
                'type':"get",
                "dataType":"json",
                success:function(res){
                  var myChart = echarts.init(document.getElementById('m3'));
                  var option = {
                      title: {
                        text: '电脑配置',
                        subtext: '价格分析',
                        left: 'center'
                      },
                      tooltip: {
                        trigger: 'item'
                      },
                      legend: {

                        left: 'left',
                        bottom:0,
                      },
                      series: [
                        {
                          name: '价格',
                          type: 'pie',
                          radius: '50%',
                          center: ['50%', '27%'],
                          data: [
                            { value: 1048, name: 'Search Engine' },
                            { value: 735, name: 'Direct' },
                            { value: 580, name: 'Email' },
                            { value: 484, name: 'Union Ads' },
                            { value: 300, name: 'Video Ads' }
                          ],
                          emphasis: {
                            itemStyle: {
                              shadowBlur: 10,
                              shadowOffsetX: 0,
                              shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                          }
                        }
                      ]
};
                  option.series[0].data=res.data;
                  if(res.status){
                      myChart.setOption(option);
                  }
                }
            })

        }

    </script>
{% endblock %}
